package com.obas.business.analogy.service.impl;

import java.util.Date;

import com.ease.common.vo.MapParameter;
import com.obas.business.analogy.service.KpiBaseAnalogyProcessService;
import com.obas.business.analogy.util.AnalogyTableNameUtil;
import com.obas.business.analogy.vo.AnalogyContextParam;
import com.obas.model.type.AnalogyDimensionAttributeType;
import com.obas.model.type.AnalogyValueAttributeType;
import com.obas.model.type.DimensionAttributeType;
import com.obas.model.type.ProcessDimensionType;

public class KpiBaseAnalogyProcessServiceImpl extends AbstractAnalogyProcessServiceImpl<AnalogyContextParam> implements KpiBaseAnalogyProcessService {

	static class Operation {
		static String extract = "extract";
		
		static String aggregate = "aggregate";

		static String accumulateAggregate = "accumulateAggregate";
	}

	public void process(AnalogyContextParam analogyContextParam) {

		analogyContextParam.setProcessDimensionType(ProcessDimensionType.KPI_BASE);

		DimensionAttributeType[] dimensionAttributeTypes = AnalogyDimensionAttributeType.getByAnalogyDimensionType(analogyContextParam.getAnalogyDimensionType());

		String tableName = AnalogyTableNameUtil.tableName(analogyContextParam);
		Date batchTimestamp = analogyContextParam.getBatchTimestamp();

		MapParameter parameter = new MapParameter();
		parameter.put("tableName", tableName);
		parameter.put("sequenceName", AnalogyTableNameUtil.sequenceName(tableName));

		String sourceTableName =null;// AnalogyTableNameUtil.tableName(analogyContextParam.getAnalogyDimensionType(), analogyContextParam.getTimeDimensionType(), ProcessDimensionType.RAW);
		parameter.put("sourceTableName", sourceTableName);
		parameter.put("dimensionAttributeTypes", dimensionAttributeTypes);
		parameter.put(AnalogyValueAttributeType.BATCHTIMESTAMP.getName(), batchTimestamp);

		this.getJdbcTemplateDataAccessObject().execute(statement(Operation.extract, parameter), parameter);

		this.getJdbcTemplateDataAccessObject().execute(statement(Operation.aggregate, parameter), parameter);

		this.getJdbcTemplateDataAccessObject().update(statement(Operation.accumulateAggregate, parameter), parameter);
	}

}
